package com.aerozhonghuan.logisticsmall.modules.home;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.modules.common.entity.GoodsObject;
import com.aerozhonghuan.logisticsmall.modules.home.beans.ActivityListBean;
import com.aerozhonghuan.logisticsmall.utils.CommunityTool;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ChenLin on 2019/5/20 16:06.Email&WeChat:7877409@163.com
 */
public class HomePageProductListAdapter extends BaseAdapter {

    private RequestOptions options;
    List<ActivityListBean.Data> mList = new ArrayList<>();
    View.OnClickListener imgClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ActivityListBean.Data entity = (ActivityListBean.Data) view.getTag();
            Bundle bundle = new Bundle();
            bundle.putString("id", entity.id + "");
            ActivityUtils.startActivity(bundle, ActivityDetailActivity.class);
        }
    };
    View.OnClickListener moreClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ActivityListBean.Data entity = (ActivityListBean.Data) view.getTag();
            Bundle bundle = new Bundle();
            bundle.putString("id", entity.id + "");
            ActivityUtils.startActivity(bundle, ActivityDetailActivity.class);
        }
    };
    private Activity mContext;

    public HomePageProductListAdapter(Activity context) {
        mContext = context;
        options = new RequestOptions().placeholder(R.mipmap.img_vp_nomal).error(R.mipmap.img_vp_nomal);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View covertView, ViewGroup arg2) {
        Holder holder = null;
        if (covertView == null) {
            holder = new Holder();
            covertView = LayoutInflater.from(mContext).inflate(R.layout.homepage_product_list_item, null);
            holder.categoryName = (TextView) covertView.findViewById(R.id.category_name);
            holder.moreView = (TextView) covertView.findViewById(R.id.more);
            holder.imageView = (ImageView) covertView.findViewById(R.id.image);
            holder.productGridView = (GridView) covertView.findViewById(R.id.product_gridview);
            holder.homePageListGridAdapter = new HomePageListGridAdapter(mContext);
            holder.productGridView.setAdapter(holder.homePageListGridAdapter);
            covertView.setTag(holder);
        } else {
            holder = (Holder) covertView.getTag();
        }
        ActivityListBean.Data entity = mList.get(position);
        List<GoodsObject> productList = entity.plist;
        holder.categoryName.setText(entity.title);
        holder.imageView.setVisibility(View.VISIBLE);
        String imgUrl = CommunityTool.changeBannerImgId("http://mall-dev.dev188.cn/fs/", entity.img, "");
        LogUtils.i(imgUrl);
        Glide.with(mContext).load(imgUrl).apply(options).into(holder.imageView);
        holder.moreView.setTag(entity);
        holder.moreView.setOnClickListener(moreClickListener);
        if (productList == null || productList.size() == 0) {
            holder.moreView.setVisibility(View.INVISIBLE);
        } else {
            holder.moreView.setVisibility(View.VISIBLE);
        }
        holder.homePageListGridAdapter.setData(productList);
        holder.imageView.setTag(entity);
        holder.imageView.setOnClickListener(imgClickListener);
        return covertView;
    }

    public void setData(ActivityListBean list) {
        if (list.datalist == null || list.datalist.size() == 0) {
            return;
        }
        mList = list.datalist;
        notifyDataSetChanged();
    }

    class Holder {
        TextView line;
        TextView categoryName;
        TextView moreView;
        ImageView imageView;
        GridView productGridView;
        HomePageListGridAdapter homePageListGridAdapter;
    }
}
